﻿using System;
using System.Data;
using Besture.Common;
using Besture.Dal;
using Besture.Model;

namespace Besture.Bll
{
    public partial class RoomDac : BaseFacade
    {
        //private readonly RoomDal excuteDal = new RoomDal();

        /// <summary>
        /// 添加一条数据
        /// </summary>
        /// <param name="model">数据模型</param>
        /// <returns></returns>
        public int Add(RoomModel model)
        {
            try
            {
                base.CreateConnection(); 
                base.BeginTransaction();
                RoomDal excuteDal = new RoomDal();
                int lsRecord = 0;
                lsRecord = excuteDal.Add(model);
                if (lsRecord > 0)
                {
                    base.Commit();
                    return lsRecord;
                }
                else
                {
                    base.RollBack();
                    return -1;
                }
            }
            catch (Exception e)
            {
                base.RollBack();
                throw e;
            }
            finally
            {
                base.CloseConnection();
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <param name="uploadMultiPhotoModel"></param>
        /// <returns></returns>
        public int Add(RoomModel model,UploadMultiPhotoModel uploadMultiPhotoModel)
        {
            try
            {
                ImageDal imgDac = new ImageDal();
                ImageModel imgModel = null;
                bool fa = false;
                int lsAddRecord = 0;
                base.CreateConnection();
                base.BeginTransaction();
                RoomDal excuteDal = new RoomDal();
                int lsRecord = 0;
                lsRecord = excuteDal.Add(model);
                if (lsRecord > 0)
                {
                    #region 多图操作
                    //增加多图
                    if (uploadMultiPhotoModel.AddImgUrlArgs != null && uploadMultiPhotoModel.AddImgRemarParamIDkArgs != null)
                    {
                        for (int i = 0; i < uploadMultiPhotoModel.AddImgUrlArgs.Length; i++)
                        {
                            fa = true;
                            //删除的记录
                            if (uploadMultiPhotoModel.DelIdArgs != null)
                            {
                                for (int j = 0; j < uploadMultiPhotoModel.DelIdArgs.Length; j++)
                                {
                                    if (uploadMultiPhotoModel.AddImgRemarParamIDkArgs[i].Substring(6, uploadMultiPhotoModel.AddImgRemarParamIDkArgs[i].Length - 6) == uploadMultiPhotoModel.DelIdArgs[j])
                                    {
                                        fa = false;
                                    }
                                }
                            }

                            if (fa)
                            {
                                imgModel = new ImageModel();
                                imgModel.HotelsID = lsRecord;
                                imgModel.ImageUrl = uploadMultiPhotoModel.AddImgUrlArgs[i];
                                imgModel.ImageRemark = System.Web.HttpContext.Current.Request.Params[uploadMultiPhotoModel.AddImgRemarParamIDkArgs[i]];
                                imgModel.CreateTime = DateTime.Now;
                                imgModel.UpdateTime = DateTime.Now;
                                imgModel.ImageType = Constants.IMAGE_TYPE_ROOM;

                                lsAddRecord = imgDac.Add(imgModel);
                            }
                        }
                    }
                    #endregion

                    if (lsAddRecord >= 0)
                    {
                        base.Commit();
                        return lsRecord;
                    }
                    else
                    {
                        base.RollBack();
                        return -1;
                    }
                }
                else
                {
                    base.RollBack();
                    return -1;
                }
            }
            catch (Exception e)
            {
                base.RollBack();
                throw e;
            }
            finally
            {
                base.CloseConnection();
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <param name="model">数据模型</param>
        /// <returns></returns>
        public int Update(RoomModel model) 
        {
            try
            {
                base.CreateConnection();
                base.BeginTransaction();
                RoomDal excuteDal = new RoomDal();
                int lsRecord = 0;
                lsRecord = excuteDal.Update(model);
                if (lsRecord > 0)
                {
                    base.Commit();
                    return lsRecord;
                }
                else
                {
                    base.RollBack();
                    return -1;
                }
            }
            catch (Exception e)
            {
                base.RollBack();
                throw e;
            }
            finally
            {
                base.CloseConnection();
            }
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <param name="uploadMultiPhotoModel"></param>
        /// <returns></returns>
        public int Update(RoomModel model, UploadMultiPhotoModel uploadMultiPhotoModel)
        {
            try
            {
                base.CreateConnection();
                base.BeginTransaction();
                RoomDal excuteDal = new RoomDal();
                int lsRecord = 0;
                ImageDal imgDac = new ImageDal();
                ImageModel imgModel = null;
                bool fa = false;
                int lsAddRecord = 0;
                lsRecord = excuteDal.Update(model);
                if (lsRecord > 0)
                {
                    #region 多图操作
                    //删除图片处理
                    if (uploadMultiPhotoModel.DelIdArgs != null)
                    {
                        for (int i = 0; i < uploadMultiPhotoModel.DelIdArgs.Length; i++)
                        {
                            string delID = uploadMultiPhotoModel.DelIdArgs[i];
                            if (delID.IndexOf(DateTime.Now.Year.ToString(), StringComparison.Ordinal) == 0)
                            {
                                continue;
                            }
                            lsAddRecord = imgDac.Delete(Convert.ToInt32(uploadMultiPhotoModel.DelIdArgs[i]));
                        }
                    }
                    //增加商品图片处理
                    if (uploadMultiPhotoModel.AddImgUrlArgs != null && uploadMultiPhotoModel.AddImgRemarParamIDkArgs != null)
                    {
                        for (int i = 0; i < uploadMultiPhotoModel.AddImgUrlArgs.Length; i++)
                        {
                            fa = true;
                            if (uploadMultiPhotoModel.DelIdArgs != null)
                            {
                                //删除的记录
                                for (int j = 0; j < uploadMultiPhotoModel.DelIdArgs.Length; j++)
                                {
                                    if (uploadMultiPhotoModel.AddImgRemarParamIDkArgs[i].Substring(6, uploadMultiPhotoModel.AddImgRemarParamIDkArgs[i].Length - 6) == uploadMultiPhotoModel.DelIdArgs[j])
                                    {
                                        fa = false;
                                    }
                                }
                            }

                            if (fa)
                            {
                                imgModel = new ImageModel();
                                imgModel.HotelsID = model.ID;
                                imgModel.ImageUrl = uploadMultiPhotoModel.AddImgUrlArgs[i];
                                imgModel.ImageRemark = System.Web.HttpContext.Current.Request.Params[uploadMultiPhotoModel.AddImgRemarParamIDkArgs[i]] ?? "";
                                imgModel.CreateTime = DateTime.Now;
                                imgModel.UpdateTime = DateTime.Now;
                                imgModel.ImageType = Constants.IMAGE_TYPE_ROOM;

                                lsAddRecord = imgDac.Add(imgModel);
                            }

                        }
                    }
                    //已经存在图片处理

                    if (uploadMultiPhotoModel.ImgIdArgs != null || uploadMultiPhotoModel.ImgUrlArgs != null)
                    {
                        for (int i = 0; i < uploadMultiPhotoModel.ImgUrlArgs.Length; i++)
                        {
                            imgModel = new ImageModel();
                            imgModel.ID = Convert.ToInt32(uploadMultiPhotoModel.ImgId[i]);
                            imgModel.HotelsID = model.ID;
                            imgModel.ImageUrl = uploadMultiPhotoModel.ImgUrlArgs[i];
                            imgModel.ImageRemark = System.Web.HttpContext.Current.Request.Params[uploadMultiPhotoModel.ImgRemarParamIDkArgs[i]] ?? "";
                            imgModel.UpdateTime = DateTime.Now;
                            imgModel.CreateTime = DateTime.Now;
                            imgModel.ImageType = Constants.IMAGE_TYPE_ROOM;

                            lsAddRecord = imgDac.Update(imgModel);
                        }
                    }
                    #endregion

                    if (lsAddRecord >= 0)
                    {
                        base.Commit();
                        return lsRecord;
                    }
                    else
                    {
                        base.RollBack();
                        return -1;
                    }
                }
                else
                {
                    base.RollBack();
                    return -1;
                }
            }
            catch (Exception e)
            {
                base.RollBack();
                throw e;
            }
            finally
            {
                base.CloseConnection();
            }
        }

        
        /// <summary>
        /// 获取数据模型
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
       public RoomModel GetModel(int id)
        {
            try
            {
                base.CreateConnection();
                RoomDal excuteDal = new RoomDal();
                return excuteDal.GetModel(id);
            }
            catch (Exception e)
            {
                throw e;
            }
           finally
            {
                 CloseConnection();
            }
        }


      public DataTable GetRoomByIDorHotelsId(int iId, int iHotelsId)
       {
           try
           {
               base.CreateConnection();
               RoomDal excuteDal = new RoomDal();
               return excuteDal.GetRoomByIDorHotelsID(iId,iHotelsId);
           }
           catch (Exception e)
           {
               throw e;
           }
           finally
           {
               CloseConnection();
           }
       }
        /// <summary>
       /// 获取全部数据
       /// </summary>
       /// <returns></returns>
       public DataTable GetDataTable()
        {
            try
            {
                base.CreateConnection();
                RoomDal excuteDal = new RoomDal();
                return excuteDal.GetDataTable();
            }
            catch (Exception e)
            {
                throw e;
            }
           finally
            {
               CloseConnection();
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public int Delete(int id)
        {
            try
            {
                base.CreateConnection();
                RoomDal excuteDal = new RoomDal();
                return excuteDal.Delete(id);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
               CloseConnection();
            }
        }

        
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="idList">主键值集合</param>
        /// <returns></returns>
        public int DeleteList(string ids)
        {
            try
            {
                base.CreateConnection();
                RoomDal excuteDal = new RoomDal();
                return  excuteDal.DeleteList(ids);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
              CloseConnection();
            }
        }
    }
}
